package com.bbcare.comm;

import com.baidu.aip.ocr.AipOcr;

/**
 * @author wuyz
 * @date 2018-03-30
 * @param 构造单例模式，获取百度云图片文字解析access_token，避免重复获取
 * @param 双重校验锁单例
 * @version v.1.0
 **/
public class AipOcrSingleton {
	
    private static volatile AipOcrSingleton instance = null;  
    private AipOcrSingleton(){}  
    public static AipOcrSingleton getInstance() {  
        if (instance == null) {  
            synchronized (AipOcrSingleton.class) {  
                if (instance == null) {//2  
                    instance = new AipOcrSingleton();  
                }  
            }  
        }  
        return instance;  
    } 
  
    /**
     * @author wuyz
     * @date 2018-03-30
     * @param 获取图像识别 access_token
     * @version v.1.0
     **/
    public AipOcr getAipOcrClient () {
    	
    	//设置APPID/Api Key/Secret Key 
    	//常量APP_ID在百度云控制台中创建，常量API_KEY与SECRET_KEY是在创建完毕应用后，
    	//系统分配给用户的，均为字符串，用于标识用户，为访问做签名验证，可在AI服务控制台中的应用列表中查看
    	//地址 https://ai.baidu.com/
        String APP_ID = "";
        String API_KEY = "";
        String SECRET_KEY = "";
    	
    	//从配置文件中获取APPID/Api Key/Secret Key 
    	APP_ID = PropertiesUtils.getProperty("APP_ID");
    	API_KEY = PropertiesUtils.getProperty("API_KEY");
    	SECRET_KEY = PropertiesUtils.getProperty("SECRET_KEY");
    	System.out.println("APP_ID"+APP_ID +"  "+API_KEY+"  "+SECRET_KEY );
    	AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
    	// 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    	return client;
    }
    
}
